﻿var g_lookstr='';
var g_sendflag = false;

function SetCookie(name, value) {
    var hours = 1;                          //此 cookie 将被保存 1 小时
    var exp = new Date();                   //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + hours * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function GetCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;
}

function Tip(rid) {
    var cookieValue = GetCookie('plugnet_' + rid);
    if(cookieValue==null)
    {
        SetCookie('plugnet_' + rid, rid);
        var gid = $.trim($('#hidSysModelId').val());
        $.post('/Tools/Ajax.ashx', { a: 'tip_c', gid: gid, rid: rid, rnd: Math.random().toString() }, function (data) {
            if (data.success) {
                $('#tp' + rid).html('顶[' + data.count + ']');
            } else {
                alert('顶贴失败：' + data.error);
            }
        },'json'); 
    }
    else
    {
        alert('您已投过一次票了！');
    }
}


function Reply(strValue,strUserName)
{
    $('#hidReplyId').val(strValue);
    $('#txtReply').focus();
    $('#replyName').html('(盖楼-' + strUserName + '-' + strValue + ')　');
}

function BtnSubmit() {
    if (arguments.length == 2 && arguments[0] == 'iframe') {
        if ($.browser.msie) {
            eval('document.frames["' + arguments[1] + '"].AttachSubmit()');
        } else {
            eval('document.getElementById("' + arguments[1] + '").contentWindow.AttachSubmit()');
        }
    }

    var error = '';
    var rid = $.trim($('#hidReplyId').val());
    var aid = $.trim($('#hidArticeId').val());
    var gid = $.trim($('#hidSysModelId').val());
    var rnd = $.trim($('#captcha').val());
    var username = '匿名';
    var content = $.trim($('#txtReply').val());

    if (g_sendflag) {
        error += '您已经发表了评论！\n';
    }
    if (aid == '') {
        error += '没有引用文章id！\n';
    }
    if (content.length > 800) {
        error += '您输入评论的字符数大于800！\n';
    }
    if (content == '') {
        error += '您输入评论的字符为空！\n';
    }
    if (rnd == '点击显示' || rnd == '') {
        error += '请输入验证码！';
    }
    if (error == '') {
        g_sendflag = true;
        $("#btnComment").attr("disabled", false);
        $.ajax({
            type: 'POST',
            dataType: "json",
            url: '/Tools/Ajax.ashx',
            data: { a: 'add_c', gid: gid, aid: aid, rid: rid, rnd: rnd, username: username, content: content },
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            success: function (data) {
                if (data.success) {
                    alert('评论成功！');
                    document.location.reload();
                } else {
                    alert('评论失败：' + data.error);
                }
            },
            error: function (data) {
                alert('评论失败：' + data);
            }
        });	
    }
    else {
        alert(error);
    }
}

$(document).ready(function () {
    $('#a_comment').hover(function () {
        if ($('#a_comment').html().indexOf('评论数目') < 0) {
            if (g_lookstr == '') {
                var aid = $.trim($('#hidArticeId').val());
                var gid = $.trim($('#hidSysModelId').val());
                $('#a_comment').html('正在获取评论数目...');
                $.get('/Tools/Ajax.ashx', { a: 'sum_c', gid: gid, aid: aid, rnd: Math.random().toString() }, function (data) {
                    if (data.success) {
                        g_lookstr = '评论数目[' + data.count + ']';
                    } else {
                        g_lookstr = data.error;
                    }
                    $('#a_comment').html(g_lookstr);
                }, 'json'); 
            }
            else {
                $('#a_comment').html(g_lookstr);
            }
        }
    }, function () {
        $('#a_comment').html('查看评论');
    });
    $('#a_comment').click(function () {
        var aid = $.trim($('#hidArticeId').val());
        $('#a_comment').attr('href', '/comment-' + aid + '-1.aspx');
    });
    $('#captcha').focus(function () {
        if ($('#captchapng').attr('src').indexOf('loading.gif') != -1) {
            var rnd = Math.random();
            $('#captcha').val('');
            $('#captchapng').show();
            $('#captchapng').attr('src', '/Tools/VerifyImager.ashx?rnd=' + rnd);
        }
    });
    $('#captchapng').click(function () {
        var rnd = Math.random();
        $('#captchapng').attr('src', '/Tools/VerifyImager.ashx?rnd=' + rnd);
    });
});